
app.controller('menuEditorCtrl', function($element, $scope, $state, rest, user, uploadConf, tipService, $window, $rootScope) {
	$scope.iNowData = angular.copy($scope.ngDialogData, {});
    console.log($scope.iNowData)
    $scope.init = function(){
    	//菜单类型
    	$scope.menuTypeList = [
    		{
    			"menuType" : "INTERNAL",
    			"name" : "内部"
    		},
    		{
    			"menuType" : "EXTERNAL",
    			"name" : "外部"
    		}
    	];
    	angular.forEach($scope.menuTypeList,function(data,index,array){
    		if(data.menuType == $scope.iNowData.menuType){
    			$scope.menuTypeVal = data;  //匹配当前点进去对应的菜单类型
    		}
    	})
    	
    	//自定义方法状态数据
		$scope.methodsStatus = [
		        {
		            "status" : "AVAILABLE",
		            "menuName" : "可用"
		        },
		        {
		            "status" : "FORBID",
		            "menuName" : "禁止"
		        },
		        {
		            "status" : "DELETE",
		            "menuName" : "删除"
		        }
		    ];
		angular.forEach($scope.methodsStatus,function(data,index,array){
			if(data.status == $scope.iNowData.status){
				$scope.itemStatus = data; //默认为可用
			}
		})
		
    	var token = $window.localStorage.getItem("token");
    	rest.decoJava({
		   	 type: "POST",
		     url: "/tenancy-sys/admin/menu/all/list",
		     data: '',
		     //contentType:"application/json",
		     beforeSend: function(request){
				request.setRequestHeader("token", token); 
			},
		    success: function(data){ 
		    	
		    	$scope.upMenuDatas = [{menuName: "请选择", id: ""}];
                angular.forEach(data.result,function(item){
                	if(item.level==0){
                		$scope.upMenuDatas.push(item);
                	}
                })
		    	
			    //$scope.upMenuDatas = data.result;
			    //$scope.upMenuDatas.unshift({menuName: "请选择", id: ""});
			    $scope.menuLookup = $scope.upMenuDatas[0];
			    $scope.$apply()
			    angular.forEach($scope.upMenuDatas,function(data){
			    	if(data.id == $scope.iNowData.upId){
			    		$scope.$apply(function(){
		            		$scope.menuLookup = data; //匹配当前点进去的所属菜单
		                })
			    	}
			    })
	           angular.element("#txt_boostrap_icon").iconPicker();  //启用图标选择差距安
		    }
	    });
    	
    };
    $scope.init();
    
    //保存
    $scope.preserveSubmit = function(){
		if($scope.menuLookup == undefined){
    		var menuLookupId = "";  //如果是模块点进来的，即没有所属菜单，为避免保存报错，设置为空值
    	} else{
    		var menuLookupId = $scope.menuLookup.id;  //非模块近来的有所属菜单
    	};
    	var menuIconCss = angular.element("#txt_boostrap_icon").val();  //图标选择插件必须要用jq去获取value，ng-model是绑定不到的
    	var formData = {
    		"id" : $scope.iNowData.id,
    		"menuName" : $scope.iNowData.menuName,
    		"menuIconCss" : menuIconCss,
    		"sort" : $scope.iNowData.sort,
    		"upId" : menuLookupId,
    		"menuPath" : $scope.iNowData.menuPath,
    		"menuType" : $scope.menuTypeVal.menuType,
    		"remark" : $scope.iNowData.remark,
    		"level": $scope.iNowData.level,
    		//"status": $scope.iNowData.status,
    		"tagName":  $scope.iNowData.tagName,
    		"status": $scope.itemStatus.status
    	}
    	var token = $window.localStorage.getItem("token");
    	rest.decoJava({
    		type: 'post',
            url: '/tenancy-sys/admin/menu/update',
            data: JSON.stringify(formData),
            contentType:"application/json; charset=utf-8",
            beforeSend: function(request){
				request.setRequestHeader("token", token); 
			},
            success: function (result) {
            	layer.closeAll('loading');
                if (result) {
                    if ("0000" == result.code) {
						layer.msg(result.message, {
				        	offset:'t',
				        	icon: 1,
							time: 2000, //不自动关闭
						});
						$scope.closeThisDialog();
                    } else {
                    	layer.msg(result.message,{
		    				offset:'t'
		    			});
                    }
                }
            }
        });
    };
    
    $scope.hideBtn = function(){
    	$scope.closeThisDialog();
    }
    
});
